﻿Public Class frmNewUser

    Private Sub btncancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncancel.Click
        Me.Close()
    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        loademp()
        loadgroup()
        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Private Sub loademp()
        Dim sql = "select employee_name,employee_id from tbl_employee where active=true"
        cboemp.ValueMember = "employee_id"
        cboemp.DisplayMember = "employee_name"
        cboemp.DataSource = Cmd.ExecuteDataTable(sql)
    End Sub

    Private Sub loadgroup()
        Dim sql = "select group_id,group_name from tbl_group where active=true"
        cbogroup.ValueMember = "group_id"
        cbogroup.DisplayMember = "group_name"
        cbogroup.DataSource = Cmd.ExecuteDataTable(sql)
    End Sub

    Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
        If txtusername.Text.Trim = "" Then
            myMsgbox.show("Please input valid username", "User")
            txtusername.Focus()
            txtusername.SelectAll()
            Exit Sub
        End If
        If txtpwd.Text.Trim = "" Then
            myMsgbox.show("Password cannot be empty", "User")
            txtpwd.Focus()
            txtpwd.SelectAll()
            Exit Sub
        End If
        If txtrepwd.Text <> txtpwd.Text Then
            myMsgbox.show("Re-Password and Password canot be different", "User")
            txtrepwd.Focus()
            txtrepwd.SelectAll()
            Exit Sub
        End If
        ClassUser.AddUser(cboemp.SelectedValue, cbogroup.SelectedValue, txtusername.Text, Crypto.Encrypt(txtpwd.Text, 8), IIf(checklogon.Checked, 1, 0))
        Me.Close()
    End Sub

    Private Sub cboemp_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboemp.SelectedIndexChanged
        loadempData()
    End Sub
    Private Sub loadempData()
        Dim dt As New DataTable
        Cmd.Parameters("@emp") = cboemp.SelectedValue
        Dim sql = "select sex, dob,address from tbl_employee where employee_id=@emp"
        dt = Cmd.ExecuteDataTable(sql)
        txtsex.Text = dt.Rows(0).Item("sex")
        Dim da As Date = dt.Rows(0).Item("dob")
        txtdob.Text = da.ToString("dd - MMM - yyyy")
        txtadr.Text = dt.Rows(0).Item("address")
    End Sub
End Class